package org.Lin.client;

import java.io.*;
import java.net.*;

public class NetworkClient {

    private static final String SERVER_IP = "101.37.88.63";
    private static final int SERVER_PORT = 5545;

    public static void send(String message) {
        new Thread(() -> {
            try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);
                 PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
                out.println(message);
                System.out.println("已发送消息: " + message);

                // 接收响应
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String response = in.readLine();
                System.out.println("收到响应: " + response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static void receive() {
        new Thread(() -> {
            try (ServerSocket serverSocket = new ServerSocket(5546)) {
                while (true) {
                    Socket socket = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String response = in.readLine();
                    System.out.println("收到服务器消息: " + response);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static void main(String[] args) {
        receive(); // 启动接收线程
    }
}
